package File;

import java.io.*;

public class TestFile {
    public static void main(String[] args) throws Exception {
        writerByFileWriter();
        readByFileReader();
    }

    public static void writerByFileWriter(){
        File file = new File("a/d.txt");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            Writer writer = new FileWriter(file,true);
            writer.append("你是个王八蛋偷吃国家大米饭,头柯南");
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void readByFileReader(){
        File file = new File("a/d.txt");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            Reader reader = new FileReader(file);
            char [] str = new char[1024];
            int read = reader.read(str);
            System.out.println(read);
            System.out.println(str);
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void readByInputStream() throws IOException {
        File file = new File("a/d.txt");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        InputStream in = new FileInputStream(file);
        try {
            byte[] str = new byte[1024];
            int len = in.read(str);
            System.out.println("len:"+len);
            String msg = new String(str);
            System.out.println(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            in.close();
        }
    }

    public static void writerByOutputStream() throws IOException {
        File file = new File("a/d.txt");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        OutputStream out = new FileOutputStream(file,true);
        try {
            out.write("you are a joker,你就是一个小丑/n/r".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
    }
}
